'''
只有贿赂，不存在奖励
'''
'''
计算投票，增加分数权重,奖励为0.2，贿赂为0.2,0.4,0.6的情况下
'''

import random


def get_random(bribe):
    i = random.random()
    if i <bribe:    #被贿赂
        return 0
    else:
        return 1



def get_vote(people,bribes):
    a=[]     #投票结果
    for i in range(people):
        a.append(get_random(bribes[i]))    #根据比例生成随机数
    return a



def vote(Bribes, people, round, weight,growth):
    weights = [weight] * people     #每个人的初始权重
    bribes = [Bribes] * people     #贿赂
    a=get_vote(people,bribes)    #第一轮投票
    result = []    #要输出的所有未被贿赂的票的概率
    for i in range(len(a)):
        if a[i] == 0:
            bribes[i] = (bribes[i] + growth) * (bribes[i] + growth)
    result.append(a.count(1)/(a.count(0)+a.count(1)))
    print('好的节点：{}'.format(a.count(1) ))
    print('被举报的坏的节点：{}'.format(a.count(0)))

    #之后投票
    for time in range(1,round+1):
        a = get_vote(people, bribes)  # 投票
        for i in range(len(a)):
            if a[i] == 0:  # 举报添加奖励因子和权重
                bribes[i] = (bribes[i] + growth*2.8) * (bribes[i] + growth*2.8)
        result.append(a.count(1)/(a.count(1)+a.count(0)))
        print('好的节点：{}'.format(a.count(1)))
        print('被举报的坏的节点：{}'.format(a.count(0)))
    return result


if __name__ == '__main__':
    rate = []        #此时为举报率
    weight = 50
    people = 1000
    round = 20
    bribes = [0.2,0.4,0.6]
    growth = 0.1
    for bribe in bribes:
        print('贿赂为{}的情况下：'.format(bribe))
        a = vote(bribe, people, round, weight,growth)
        print('{}\n'.format(a))


